import os
import sys
from selenium import webdriver
from time import sleep
from selenium.webdriver.common.by import By
from selenium.webdriver.firefox.service import Service as FirefoxService
from selenium.webdriver.common.action_chains import ActionChains
class LikeShopTest():
    def __init__(self) -> None:

        driver_path = os.path.join(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(sys.executable)))),
                               'geckodriver.exe')
        service = FirefoxService(executable_path=driver_path)
        options = webdriver.FirefoxOptions()
        self.browser = webdriver.Firefox(service=service, options=options)

        def test(self):
            self.browser.get("http://work:8081/admin/")
            sleep(1)
            self.browser.find_element(By.NAME,'account').send_keys('root')
            self.browser.find_element(By.NAME,'password').send_keys('root')
            self.browser.find_element(By.ID,'login').click()
            sleep(2)
        def test_shop_list(self):
            # self.browser.find_element(By.XPATH,'/html/body/div[1]/div/div[2]/div/div/div[1]/div/div[5]/div[1]/div/div/div/input')
            
            self.browser.find_element(By.XPATH,"/html/body/div/div[1]/div[2]/div/ul/li[1]/a/cite").click()
            sleep(3)
            aa = self.browser.find_elements(By.XPATH,"/html/body/div/div/div[2]/div/div/div[2]/div[2]/div[1]/div[2]/table/tbody/tr")
            is_bug_true=True
            if len(aa)==0:
                print("无数据")
            else:
                
                
                for tr in aa:
                    td = tr.find_element(By.XPATH,'./td[2]/div')
                    title = td.text
                    if '文具' not in title:
                        print('有Bug')
                        is_bug_true=False
            if is_bug_true :
                print("没有Bug")
                
                
            self.browser.switch_to.default_content()
            dt =self.browser.find_element(By.XPATH,'/html/body/div/div[1]/div[1]/ul[2]/li[3]/a')
            ActionChains(self.browser).move_to_element(dt).perform()

            #鼠标悬停
            ActionChains(self.browser).move_to_element(dt).perform()
            print("悬停")
            sleep(3)
            self.browser.find_element(By.XPATH,'/html/body/div/div[1]/div[1]/ul[2]/li[3]/dl/dd[2]/a').click()
                  def test_shop_add(self):
        """商品新增"""
         #点击商品
        self.brower.find_element(By.XPATH,'/html/body/div/div[1]/div[2]/div/ul/li[1]/a').click()
        sleep(1)
        #查找字框架
        iframe1 = self.browser.find_element(By.XPATH,'/html/body/div/div[3]/div[2]/iframe')
        #进入子框架
        self.browser.switch_to.frame(iframe1)
        #定位发布商品按钮并点击
        self.brower.find_element(By.XPATH,'/html/body/div/div/div[2]/div/div/div[2]/div[1]/button[1]').click()
        sleep(2)
        #查找商品发布子框架
        iframe_add = self.browser.find_element(By.XPATH,'//*[@id="layui-layer-iframe1"]')
        #进入商品发布子框架
        self.browser.swich_to.frame(iframe_add)

        #定位商品名称输入框并输入
        now = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        self.browser.find_element(By.NAME,'name').send_keys('测试商品'+now)
        sleep(1)
        #定位第一个分类的选项框并点击
        self.brower.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[1]/div/div/input').click()
        sleep(1)
        #定位第一个分类的下拉框选项并点击
        self.brower.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[1]/div/di/dd[2]').click()
        sleep(2)

        #定位第二个分类的选项框并点击
        self.brower.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[2]/div/div/input').click()
        sleep(1)
        #定位第二个分类的下拉框选项并点击
        self.brower.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[2]/div/di/dd[2]').click()
        sleep(2)

        #定位第三个分类的选项框并点击
        self.brower.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[3]/div/div/input').click()
        sleep(1)
        #定位第三个分类的下拉框选项并点击
        self.brower.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[3]/div[3]/div/di/dd[2]').click()
        sleep(2)

        #定位商品主图的图片上传按钮并点击
        self.brower.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[5]/div/div[2]/a').click()
        sleep(2)
        self.test_upload('//*[@id="layui-layer-iframe2"]','E:\\software-test-2202\\1.jpg')
        #切换到商品发布子框架
        self.browser.swich_to.parent_frame(iframe_add)
        sleep(2)
        #点击商品轮播图的图片上传按钮
        self.brower.find_element(By.XPATH,'/html/body/div/div/div[1]/div/div/div[6]/div/div/a').click()
        sleep(2)

        #切换到价格库存标签页
        self.brower.find_element(By.XPATH,'/html/body/div/ul/li[2]').click()
        sleep(2)
        #定位市场价（元）并输入
        self.brower.find_element(By.XPATH,'/html/body/div/div/div[2]/div/div/div[4]/dvi/table/tbody/tr/td[2]/input').send_keys(1)
        #定位价格（元）并输入
        self.brower.find_element(By.XPATH,'/html/body/div/div/div[2]/div/div/div[4]/dvi/table/tbody/tr/td[3]/input').send_keys(1)
        #定位成本价（元）并输入
        self.brower.find_element(By.XPATH,'/html/body/div/div/div[2]/div/div/div[4]/dvi/table/tbody/tr/td[4]/input').send_keys(1)
        #定位库存并输入
        self.brower.find_element(By.XPATH,'/html/body/div/div/div[2]/div/div/div[4]/dvi/table/tbody/tr/td[5]/input').send_keys(1)
        sleep(1)
        #切换到销售设置标签页
        self.brower.find_element(By.XPATH,'/html/body/div/ul/li[4]').click()
        sleep(2)
        #定位配送方式的快递发货并点击
        self.brower.find_element(By.XPATH,'/html/body/div/div/div[4]/div[6]/div/div[1]/span').click()
        #返回到商品子框架
        self.browser.swich_to.parent_frame()
        sleep(2)
        #定位保存按钮并点击
        self.browser.find_element(By.CLASS_NAME,'layui-layer-btn0').click()
        sleep(2)





    def test_upload(self,xpath_path,img_path):
        """图片上传"""
        #返回到商品子框架
        self.browser.swich_to.parent_frame()
        #定位图片上传框架并切换
        iframe_upload = self.browser.find_element(By.XPATH,'//*[@id="layui-layer-iframe2"]')
        self.browser.swich_to.frame(iframe_upload)

        #定位图片输入框并输入图片路径
        self.browser.find_element(By.XPATH,'/html/body/div/div[2]/div[1]/div[1]/input').send_keys(img_path)
        sleep(4)
        #选择图片列表的第一张图片
        self.browser.find_element(By.XPATH,'/html/body/div/div[2]/div[1]/div[2]/div/ul/li[1]/div/img').click()
        sleep(1)
        #点击使用选中图片按钮
        self.browser.find_element(By.XPATH,'//*[@id="used"]').click()
         #返回到商品子框架
        self.browser.swich_to.parent_frame()


    def test_shop_online(self):
        """商品上架测试"""
        #点击商品
        self.brower.find_element(By.XPATH,'/html/body/div/div[1]/div[2]/div/ul/li[1]/a').click()
        sleep(1)
        #查找字框架
        iframe1 = self.browser.find_element(By.XPATH,'/html/body/div/div[3]/div[2]/iframe')
        #进入子框架
        self.browser.switch_to.frame(iframe1)
        #定位到仓库中商品按钮并点击
        self.brower.find_element(By.XPATH,'/html/body/div[1]/div/div[2]/ul/li[2]').click()
        sleep(2)
        #接收商品列表
        shop_list = self.brower.find_element(By.XPATH,'/html/body/div[1]/div/div[2]/div/div/div[2]/div[2]/div[1]/div[3]/div[2]/table/tbody/tr')
        if len(shop_list) > 0:
            #定位到商品列表中的第一个商品并点击上架
            shop_list[0].find_element(By.XPATH,'./td/div/a[2]').click()
            sleep(2)
            #定位确认按钮并点击
            self.browser.find_element(By.CLASS_NAME,'layui-layer-btn0').click()
            sleep(2)
        else:
            print('仓库中暂无商品')
        

        
        def test_shop_online(self):
            """商品上架测试"""
            # 1. 进入商品列表页面
        self.browser.find_element(By.XPATH, '/html/body/div/div[1]/div[2]/div/ul/li[1]/a').click()
        sleep(2)
        
        # 2. 进入商品列表框架
        iframe1 = self.browser.find_element(By.XPATH, '/html/body/div/div[3]/div[2]/iframe')
        self.browser.switch_to.frame(iframe1)
        sleep(1)
        
        # 3. 获取商品列表（优先选择刚新增的测试商品）
        shop_list = self.browser.find_elements(By.XPATH, '/html/body/div/div/div[2]/div/div/div[2]/div[2]/div[1]/div[2]/table/tbody/tr')
        if not shop_list:
            print("商品列表为空，无法编辑")
            self.browser.switch_to.default_content()
            return
        
        # 4. 点击第一个商品的编辑按钮
        edit_btn = shop_list[0].find_element(By.XPATH, './td/div/a[1]')  # 假设编辑按钮是第一个a标签
        edit_btn.click()
        sleep(3)  # 等待编辑弹窗加载
        
        # 5. 切换到编辑弹窗的iframe（假设弹窗iframe的id规则与新增一致）
        edit_iframe = self.browser.find_element(By.XPATH, '//*[@id="layui-layer-iframe1"]')
        self.browser.switch_to.frame(edit_iframe)
        sleep(1)
        
        # 6. 修改商品名称（添加"编辑后"标识）
        name_input = self.browser.find_element(By.NAME, 'name')
        original_name = name_input.get_attribute('value')
        new_name = f"[编辑后]{original_name}"
        name_input.clear()
        name_input.send_keys(new_name)
        sleep(1)
        
        # 7. 修改价格（示例：在原价格基础上加10）
        self.browser.find_element(By.XPATH, '/html/body/div/ul/li[2]').click()  # 切换到价格库存标签
        sleep(2)
        price_input = self.browser.find_element(By.XPATH, '/html/body/div/div/div[2]/div/div/div[4]/div/table/tbody/tr/td[3]/input')
        original_price = price_input.get_attribute('value')
        new_price = str(float(original_price) + 10)
        price_input.clear()
        price_input.send_keys(new_price)
        sleep(1)
        
        # 8. 切换到销售设置标签页，修改配送方式（示例：切换为自提）
        self.browser.find_element(By.XPATH, '/html/body/div/ul/li[4]').click()
        sleep(2)
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[4]/div[6]/div/div[2]/span').click()  # 假设第二个选项是自提
        sleep(1)
        
        # 9. 保存修改
        self.browser.switch_to.parent_frame()  # 退出编辑弹窗iframe
        self.browser.find_element(By.CLASS_NAME, 'layui-layer-btn0').click()  # 确认保存按钮
        sleep(3)
        
        # 10. 验证修改结果（重新查询商品）
        self.browser.find_element(By.ID, 'keyword').send_keys(new_name)  # 搜索修改后的名称
        self.browser.find_element(By.XPATH, '/html/body/div/div/div[2]/div/div/div[1]/div/div[5]/div[3]/button[1]').click()
        sleep(2)
        
        # 检查是否能找到修改后的商品
        updated_shop = self.browser.find_elements(By.XPATH, '/html/body/div/div/div[2]/div/div/div[2]/div[2]/div[1]/div[2]/table/tbody/tr')
        if updated_shop:
            print(f"商品编辑成功，新名称：{new_name}，新价格：{new_price}")
        else:
            print("商品编辑失败，未找到修改后的商品")
        
        # 11. 退出框架
        self.browser.switch_to.default_content()
            
        
    
    
    
if __name__ == '__main__':
    lk =LikeShopTest()
    lk.test()
    